Skip to content

Add num_to_thaiword_float and fix เอ็ด rule in num_to_thaiword - #1461

Open
Warit-Yuv wants to merge 3 commits into
PyThaiNLP:mainfrom
Warit-Yuv:feature/num-to-thaiword-float
Open

Add num_to_thaiword_float and fix เอ็ด rule in num_to_thaiword#1461
Warit-Yuv wants to merge 3 commits into
PyThaiNLP:mainfrom
Warit-Yuv:feature/num-to-thaiword-float

Conversation

@Warit-Yuv

@Warit-Yuv Warit-Yuv commented Jul 26, 2026

Copy link
Copy Markdown
  • Fix เอ็ด rule for millions block (e.g., 101,000,000 -> หนึ่งร้อยเอ็ดล้าน)
  • Add num_to_thaiword_float for floating-point numbers
  • Refactor num_to_thaiword to process in 6-digit blocks
  • Add comprehensive tests for เอ็ด edge cases and float support
  • Update API documentation

What do these changes do

Fix the เอ็ด (one's-place) pronunciation rule in num_to_thaiword for the millions block, and add num_to_thaiword_float for floating-point number support.

What was wrong

The original algorithm processed digits right-to-left with a flat modulo-6 approach, causing the เอ็ด rule to only check the very end of the output string. This missed cases where "หนึ่ง" appeared before "ล้าน" in the middle of the string (e.g., 101,000,000 returned "หนึ่งร้อยหนึ่งล้าน" instead of "หนึ่งร้อยเอ็ดล้าน"). Additionally, there was no function to handle floating-point numbers.

How this fixes it

Rewrote num_to_thaiword to split the number into blocks of 6 digits from right, convert each block independently (applying the เอ็ด rule per block), then join with "ล้าน". Added num_to_thaiword_float which reuses num_to_thaiword for the integer part, reads the decimal point as "จุด", and reads each digit after the decimal individually.

Files changed

File Change
pythainlp/util/numtoword.py Refactored num_to_thaiword with block-based algorithm; added num_to_thaiword_float
pythainlp/util/__init__.py Exported num_to_thaiword_float
tests/core/test_util.py Added tests for เอ็ด edge cases and num_to_thaiword_float
docs/api/util.rst Added API documentation for num_to_thaiword_float

Fixes #1460

Your checklist for this pull request

  • Passed code styles and structures
  • Passed code linting checks and unit test

- Fix เอ็ด rule for millions block (e.g., 101,000,000 -> หนึ่งร้อยเอ็ดล้าน)
- Add num_to_thaiword_float for floating-point numbers
- Refactor num_to_thaiword to process in 6-digit blocks
- Add comprehensive tests for เอ็ด edge cases and float support
- Update API documentation
Copilot AI review requested due to automatic review settings July 26, 2026 08:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Thai numeral-to-text conversion utilities by fixing the “เอ็ด” rule for the millions (ล้าน) block in num_to_thaiword, and adds num_to_thaiword_float for floating-point support.

Changes:

  • Refactors num_to_thaiword to process numbers in 6-digit blocks, correctly applying the “เอ็ด” rule within each block (fixes #1460).
  • Adds num_to_thaiword_float for rendering decimals using “จุด” and per-digit reading after the decimal point.
  • Extends unit tests and API documentation to cover the new float function and “เอ็ด” edge cases.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
pythainlp/util/numtoword.py Refactors integer conversion logic and adds num_to_thaiword_float.
pythainlp/util/init.py Exposes num_to_thaiword_float from the util package.
tests/core/test_util.py Adds test cases for float support and “เอ็ด” edge cases.
docs/api/util.rst Documents the new num_to_thaiword_float API.

Comment thread pythainlp/util/numtoword.py
Comment thread tests/core/test_util.py Outdated
Comment on lines +157 to +159
# Very small float (scientific notation)
self.assertEqual(num_to_thaiword_float(1e-5), "ศูนย์จุดศูนย์ศูนย์ศูนย์ศูนย์หนึ่ง")
# Leading zeros after decimal
Warit-Yuv and others added 2 commits July 29, 2026 06:07
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@sonarqubecloud

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (4)

pythainlp/util/numtoword.py:110

  • Local loop variable name replac is unclear/abbreviated; this makes the code harder to read. Use a descriptive name like replacement.
    for search, replac in _EXCEPTIONS.items():
        output = output.replace(search, replac)

pythainlp/util/numtoword.py:179

  • num_to_thaiword_float can raise ValueError for non-finite floats (NaN/Inf), but the docstring only documents TypeError. Document ValueError as well so callers know what to handle.
    :param float number: a floating-point number to be converted to Thai text
    :return: text representing the number in Thai
    :rtype: str
    :raises TypeError: if *number* is not a numeric type

tests/core/test_util.py:180

  • The new non-finite-float handling branch (math.isfinite -> ValueError) is not covered by tests. Add assertions for inf, -inf, and nan so this behavior is locked in.
        # Type error
        with self.assertRaises(TypeError):
            num_to_thaiword_float(None)  # type: ignore[arg-type]
        with self.assertRaises(TypeError):
            num_to_thaiword_float("not a number")  # type: ignore[arg-type]

tests/core/test_util.py:162

  • Continuation lines in this multi-line assertEqual are mis-indented, which hurts readability and may trip formatting/lint rules. Align the arguments consistently.
        self.assertEqual(
             num_to_thaiword_float(1.23e-10),
             "ศูนย์จุดศูนย์ศูนย์ศูนย์ศูนย์ศูนย์ศูนย์ศูนย์ศูนย์ศูนย์หนึ่งสองสาม",
         )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: num_to_thaiword: เอ็ด rule broken for millions block, and missing float support

2 participants